<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/23
 * Time: 2:24 PM
 */
namespace app\frontend\controllers;

use app\common\exceptions\BizException;
use app\common\traits\ResponseJsonTrait;
use app\common\traits\MessageViewTrait;
use Illuminate\Routing\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;

/**
 * 前端端controller基类，要注意方法控制，不要用public方法，防止前端访问
 * Class BaseController
 * @package App\backend\controllers
 */
class BaseController extends Controller
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests,ResponseJsonTrait,MessageViewTrait;
    public function __construct()
    {
        
    }

    /**
     * url参数验证
     *
     * @param array $rules
     * @param Request|null $request
     * @param array $messages
     * @param array $customAttributes
     *
     * @throws AppException
     * @throws BizException
     */
    public function validate(array $rules, Request $request = null, array $messages = [], array $customAttributes = [])
    {
        if (!isset($request)) {
            $request = request();
        }
        $validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);

        if ($validator->fails()) {
            throw new BizException($validator->errors()->first());
        }
    }
}